﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DomainModel.Entities;
using DomainModel.Concrete;
using DomainModel.Abstract;
using Iesi.Collections;

namespace WebCore.Services
{
    public class TagService
    {
        private readonly ITagsRepository _tagRepository;

        public TagService()
        {
            _tagRepository = new TagsRepository();
        }

        public static IList<Tag> GetAllTags()
        {
            var _tagRepository = new TagsRepository();

            var tags = _tagRepository.GetAll().Where(x => x.EventsList.Count != 0).ToList();
            var tagList = tags.GroupBy(t => t.TagName.ToLower())
                              .Select(g => new Tag()
                              {
                                  TagName = g.Key,
                                  TagCount = g.Count()
                              })
                              .OrderBy(x=>x.TagName).ToList();


            return tagList;
        }


        public static string GetTagClass(double tagNumbers, double maxCount)
        {
            double weightPercent = (tagNumbers / maxCount) * 100;

            if (weightPercent >= 99)
            {
                // strongest
                return "tag5";
            }
            else if (weightPercent >= 70)
            {
                return "tag4";
            }
            else if (weightPercent >= 40)
            {
                return "tag3";
            }
            else if (weightPercent >= 20)
            {
                return "tag2";
            }
            else if (weightPercent >= 3)
            {
                //weakest
                return "tag1";
            }
            else 
            {
                return "tag0";
            }
        }
       

        public static void AddTagsToEvent(string tags, Event item)
        {
            if (!string.IsNullOrEmpty(tags))
            {
                //remove all white space
                //tags = tags.Replace(" ", string.Empty);

                //to array
                string[] tagNames = tags.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);

                if (tagNames.Length > 0)
                {
                    // remove the duplicate items
                    tagNames = Utils.RemoveDuplicateString(tagNames);
                    
                    foreach (string t in tagNames)
                    {
                        string tagName = Utils.RemoveIllegalCharacters(t, false);

                        if (!string.IsNullOrEmpty(tagName))
                        {
                            // create tag
                            var tag = new Tag
                            {
                                TagName = tagName,
                                DateCreated = DateTime.Now
                            };
                          
                            // buildup many to many relationship
                            //add the tag to event
                            item.TagsList.Add(tag); 
                            tag.EventsList.Add(item);
                        }
                    }
                }
            }
        }

    }
}
